package thinker.miracle.ams.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import thinker.miracle.ams.dao.InterfaceDao;
import thinker.miracle.ams.entity.InterfaceEntity;
import thinker.miracle.ams.model.InterfaceModel;
import thinker.miracle.ams.model.ParamsModel;
import thinker.miracle.ams.model.ResultModel;
import thinker.miracle.commons.Page;
import thinker.miracle.utils.BeanUtil;


/**
 * @author bo.chen
 *
 */
@Service
public class InterfaceService {
	
	@Autowired
	InterfaceDao interFaceDao;
	
	@Autowired
	ParamsService paramsService;
	
	@Autowired
	ResultService resultService;

	/**
	 * 保存日志信息
	 * 
	 * @param loggerEntity
	 *
	 */
	@Transactional
	public void save(InterfaceEntity interfaceEntity){
		if(interfaceEntity != null && interfaceEntity.getId() == 0){
			interFaceDao.insert(interfaceEntity);
		}else if(interfaceEntity != null && interfaceEntity.getId() > 0){
			interFaceDao.updateSelective(interfaceEntity);
		}else if(interfaceEntity == null)
			throw new RuntimeException("null piont exception");
	}
	
	/**
	 * <p>获取列表</p>
	 * 
	 * @param page
	 * @return
	 * 
	 */
	public List<InterfaceEntity> getInterfaceList(Page<InterfaceEntity> page){
		List<InterfaceEntity> results = interFaceDao.findListOrderByOrderNum(page);
		page.setResults(results);
		return results;
	}

	/**
	 * <p>获取列表（不分页）</p>
	 * 
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * 
	 */
	public List<InterfaceModel> getInterfaces() throws InstantiationException, IllegalAccessException {
		List<InterfaceEntity> interfaceEntitys = interFaceDao.findAllOrderByOrderNum();
		List<InterfaceModel> interfaceModels = BeanUtil.copyPropertiesList(interfaceEntitys, InterfaceModel.class);
		if(interfaceModels != null && interfaceModels.size() > 0){
			for( int i = 0 ;i < interfaceModels.size(); i++) {
				InterfaceModel interfaceModel = interfaceModels.get(i);
				List<ParamsModel> params = paramsService.getByInterfaceId(interfaceModel.getId());
				interfaceModel.setParams(params);
				
				List<ResultModel> results = resultService.getByInterfaceId(interfaceModel.getId());
				interfaceModel.setResults(results);
			}
		}
		return interfaceModels;
	}
}